$(function () {
    $("#selectCity").on('tap', function () {
        var picker = new mui.PopPicker({
            layer: 3
        });
        picker.setData(cityData);
        picker.show(function (selectItems) {
            // console.log(selectItems[0].text); 
            // console.log(selectItems[1].text); 
            // console.log(selectItems[2].text); 
            $("#selectCity").val(selectItems[0].text + selectItems[1].text + selectItems[2].text)
        })
    })
    // var html = template("artList",)
    //判断

    var datas = Number(substrs(location.href, 1))
    console.log(datas)
    //编辑进来
    if (datas) {
        var addList = JSON.parse(localStorage.getItem("Oedit"))
        // console.log(addList)
        var html = template("artList", addList)
        $("#allList").html(html)
        $(".mui-title").html("修改收货地址")
    }
    $("#addconfirm").on('tap', function () {
        var address = $("#selectCity").val()
        var addressDetail = $("[name='addressDatil']").val()
        var recipients = $("[name='user']").val()
        var postcode = $("[name='email']").val()

        if (!recipients) {
            mui.toast("收件人不能为空")
            return;
        }
        if (!/^\d{6}$/.test(postcode)) {
            mui.toast("请填写合法的邮政编码")
            return;
        }
        if (!address) {
            mui.toast("请选择省市区")
            return;
        }
        if (!addressDetail) {
            mui.toast("请填写详细住址")
            return;
        }
        console.log(address)
        console.log(addressDetail)
        console.log(recipients)
        console.log(postcode)
        if (datas) {
            var url = '/address/updateAddress'
            var editId = addList.id
        } else {
            var url= '/address/addAddress'
        }
        $.ajax({
            url: url,
            type: 'POST',
            data: {
                id: editId,
                address: address,
                addressDetail: addressDetail,
                recipients: recipients,
                postcode: postcode
            },

            success: function (res) {
                if (datas) {
                    mui.toast("修改收货地址成功")
                } else {
                    mui.toast("添加新收货地址成功")
                }

                setTimeout(() => {
                    location.href = "address.html"
                }, 1500)
            }
        })
    })

})

function substrs(url, name) {
    var booleans = url.substr(url.indexOf("=") + 1)
    return booleans
}